package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.exception.MyValidationException;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.utils.CommonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @PROJECT_NAME: market_34th
 * @DESCRIPTION:
 * @Author: qiu
 * @DATE: 2021/10/18 21:35
 */
@RestController
@RequestMapping("/admin/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * 管理员分页信息
     * @param param
     * @return
     */
    @RequiresPermissions(value = {"admin:admin:list"})
    @GetMapping("/list")
    public BaseRespVo list(String username, BaseParam param) {

        BasePageData<Admin> data = adminService.query(username, param);

        return BaseRespVo.ok(data,"成功");
    }

    /**
     * 添加管理员
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:create")
    @PostMapping("/create")
    public BaseRespVo create4log(@Validated @RequestBody Admin admin, BindingResult bindingResult) throws MyValidationException {

        // 参数校验
        CommonUtil.valid(bindingResult);
        // 校验成功
        admin = adminService.create4log(admin);

        return BaseRespVo.ok(admin,"成功创建管理员");
    }

    /**
     * 删除管理员
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:delete")
    @PostMapping("/delete")
    public BaseRespVo delete4log(@RequestBody Admin admin) {
        if(admin == null || admin.getId() == 1){
            return BaseRespVo.fail("初始管理员账号不能删除！");
        }
        adminService.delete4log(admin);
        return BaseRespVo.ok("成功删除管理员！");
    }

    /**
     * 修改管理员
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:update")
    @PostMapping("/update")
    public BaseRespVo update4log(@RequestBody Admin admin) {
        if(admin.getId() == 1){
            return BaseRespVo.fail("初始管理员账号不能修改！");
        }
        admin = adminService.update4log(admin);
        return BaseRespVo.ok(admin,"成功修改信息");
    }

}
